package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.tree.TreeEntity;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.SubjectMapper;
import com.atguigu.eduservice.service.SubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-01-04
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, EduSubject> implements SubjectService {


    /**
     * 添加课程分类
     *
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file, SubjectService subjectService) {
        try {
            EasyExcel.read(file.getInputStream(), SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 课程分类列表
     *
     * @return
     */
    @Override
    public List<TreeEntity> getAllSubject() {
        return buildData();
    }

    /**
     * 构建前端所需的树形结构
     *
     * @return
     */
    private List<TreeEntity> buildData() {
        List<TreeEntity> oneTrees = new ArrayList<>();
        List<EduSubject> oneSubjects = selectAllParentSubject();
        List<EduSubject> twoSubjects = selectAllTwoSubject();
        if (oneSubjects.size() > 0 && oneSubjects != null) {
            for (int i = 0; i < oneSubjects.size(); i++) {
                EduSubject oneSubject = oneSubjects.get(i);
                TreeEntity oneTree = new TreeEntity();
                BeanUtils.copyProperties(oneSubject, oneTree);
                oneTrees.add(oneTree);
                List<TreeEntity> twoTrees = new ArrayList<>();
                if (twoSubjects.size() > 0 && twoSubjects != null){
                    for (int j = 0; j < twoSubjects.size(); j++) {
                        EduSubject twoSubject = twoSubjects.get(j);
                        TreeEntity twoTree = new TreeEntity();
                        if (twoSubject.getParentId().equals(oneSubject.getId())){
                            BeanUtils.copyProperties(twoSubject, twoTree);
                            twoTrees.add(twoTree);
                        }
                    }
                }
             oneTrees.get(i).setChildren(twoTrees);
            }
        }
        return oneTrees;
    }

    /**
     * 查询所有一级分类
     *
     * @return
     */
    private List<EduSubject> selectAllParentSubject() {
        QueryWrapper<EduSubject> qw = new QueryWrapper<>();
        qw.eq("parent_id", "0");
        return baseMapper.selectList(qw);
    }

    /**
     * 查询所有二级分类
     *
     * @return
     */
    private List<EduSubject> selectAllTwoSubject() {
        QueryWrapper<EduSubject> qw = new QueryWrapper<>();
        qw.ne("parent_id", "0");
        return baseMapper.selectList(qw);
    }

}
